import { getSetting, chooseAddress, openSetting, showModal, showToast } from "../../utils/asyncWx"
import regeneratorRuntime from '../../lib/runtime/runtime'
// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },
  onShow() {
    // 获取缓存中的收货地址
    const address = wx.getStorageSync("address");
    // 获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart") || [];
    // 过滤后的购物车数组
    cart = cart.filter(v => v.checked);
    this.setData({ address });

    // 总价格总数量
    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v => {
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
    })
    // 购物车数据重新设置回data中
    this.setData({
      cart,
      totalPrice,
      totalNum,
      address
    });
  },
  // 支付
  async handlePay(){
    await showToast({title:"省点钱吧，就知道买买买！"});
  }
})